package com.homework.hugh.controller;

import com.homework.hugh.cache.GuavaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.homework.hugh.util.Constant.code_key;

/**
 * @Author: Hugh
 * @Date: 2020/10/26
 */
@RestController
@RequestMapping("/api/code")
public class CodeController {

    @Autowired
    private GuavaService guavaService;

    //验证码的key 是 code_key
    /**
     * ⽣成验证码并发送到对应邮箱
     * @param email 邮箱
     * @return 成功true，失败
     * false
     */
    @GetMapping("/create/{email}")
    public Boolean create(@PathVariable String email){
        //模拟发送
        int code = (int)((Math.random()*9+1)*100000);
        //保存
        //验证码10分钟内有效,保存到本地缓存中
        System.out.println("发送的验证码是" + code);
        guavaService.save(code_key, code);
        return Boolean.TRUE;
    }
}
